package com.fdb.efp.console.api.rest;


import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.DateUtility;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.SequenceService;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.efp.console.service.facade.CaseDistrRatioService;
import com.fdb.efp.console.service.vo.CaseDistrRatioVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class CaseDistrRatioController extends AbstractController  {
	
	@Autowired
	@Qualifier("caseDistrRatioService")
	private CaseDistrRatioService caseDistrRatioService;
	
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	@Autowired
	@Qualifier("dbSeqService")
	private SequenceService sequenceService;
	
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/case/distr/ratios", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<CaseDistrRatioVO>> queryCaseDistrRatioAll(
			@RequestBody CaseDistrRatioVO vo) {		
		List<CaseDistrRatioVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = caseDistrRatioService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = caseDistrRatioService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = caseDistrRatioService.queryAllCurrDownOrg(vo);
			break;
		case CurrOwnerPrd:
			outputVo = caseDistrRatioService.queryAllCurrOwnerPrd(vo);
			break;
		default:
			outputVo = caseDistrRatioService.queryAllCurrOrg(vo);
			break;
		}
		handleWithTime(outputVo);
		return getResponseData(outputVo);
	}

	private void handleWithTime(List<CaseDistrRatioVO> outputVo) {
		outputVo.forEach(item->{
			if (item.getLastUpdateTime()!= null && item.getLastUpdateTime() != "") {
				item.setLastUpdateTime(item.getLastUpdateTime().substring(0,19));
			}
		});
	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/case/distr/ratio", method=RequestMethod.GET)
	public @ResponseBody ResponseData<CaseDistrRatioVO> queryByPk() {		
		CaseDistrRatioVO inVo = new CaseDistrRatioVO();
		CaseDistrRatioVO outputVo = caseDistrRatioService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param caseDistrRatio
	 * @return
	 */
	@RequestMapping(value="/case/distr/ratio", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CaseDistrRatioVO inCaseDistrRatioVo) {		
		int outputVo = caseDistrRatioService.deleteByPk(inCaseDistrRatioVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inCaseDistrRatioVo
	 * @return
	 */
	@RequestMapping(value="/case/distr/ratio", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CaseDistrRatioVO inCaseDistrRatioVo) {
		setUserInfoToVO(inCaseDistrRatioVo);
		inCaseDistrRatioVo.setLastUpdateUser(inCaseDistrRatioVo.getLoginUserId());
		int outputVo = caseDistrRatioService.updateByPk(inCaseDistrRatioVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inCaseDistrRatioVo
	 * @return
	 */
	@RequestMapping(value="/case/distr/ratio", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCaseDistrRatio(@RequestBody CaseDistrRatioVO inCaseDistrRatioVo) {
		int outputVo = 0;
		if(caseDistrRatioService.getConditionSum(inCaseDistrRatioVo) > 0) {
			logger.debug("该机构还存在未处理完成的数据，请先处理！");
			outputVo = -2;
		}else {
			try {
				inCaseDistrRatioVo.setCaseDistrAppNo(sequenceService.getSequence("CASE_DISTR_APP_SERNO", null, null));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			setUserInfoToVO(inCaseDistrRatioVo);
			inCaseDistrRatioVo.setSubmUserCode(inCaseDistrRatioVo.getLoginUserId());
			inCaseDistrRatioVo.setSubTime(DateUtility.getCurrAppDateStr());
			outputVo = caseDistrRatioService.insertCaseDistrRatio(inCaseDistrRatioVo);
		}
		return getResponseData(outputVo);
		
	}

	/**
	 * 根据合作机构号删除版本状态为新增的数据
	 * @param caseDistrRatio
	 * @return
	 */
	@RequestMapping(value="/case/distr/ratio/del", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> batchDeleteByCooNo(@RequestBody CaseDistrRatioVO inCaseDistrRatioVo) {
		int outputVo = caseDistrRatioService.batchDeleteByCooNo(inCaseDistrRatioVo);
		return getResponseData(outputVo);

	}
}
